/**
 * 格式化日期
 * @param fmt 例如：yyyy-MM-dd 等
 * @returns {*}
 * @constructor
 */
function dateFormat(date, format = 'YYYY-MM-DD HH:mm:ss') {
    const config = {
        YYYY: date.getFullYear(),
        MM: date.getMonth() + 1 < 10 ? `0${date.getMonth()+1}` : date.getMonth() + 1,
        DD: date.getDate() < 10 ? `0${date.getDate()}` : date.getDate(),
        HH: date.getHours() < 10 ? `0${date.getHours()}` : date.getHours(),
        mm: date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes(),
        ss: date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds(),
    }
    // console.log(config,'config')
    for (const key in config) {
        format = format.replace(key, config[key])
    }
    return format
}

function dateFormatYMD(time) {
    let str = time
    let a = str.split("T").join(" ")
    let b = a.substring(0, 10)
    return b
}

function FormatterDate(bir) {
    //判断是否为空
    if (bir == null)
        return "";
    //替换前面的字符
    bir = bir.replace("/Date(", "");
    //替换后面
    bir = bir.replace(")/", "");
    //转换为数字
    bir = parseInt(bir);
    //转换为日期格式
    var time = new Date(bir);
    return timeStampString(time.getTime());
    //return time.toLocaleDateString();
}


function timeStampString(time) {
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

// 校验箱号
function ValidCNTNO(CNTNO) {
    var p_cntr1,
        p_cntr2,
        p_check,
        p_container,
        FValue,
        p_i,
        p_a,
        p_power,
        p_sum,
        p_check_i;
    FValue = false;
    //如果箱号长度不为11，肯定错误
    if (CNTNO.length != 11) {
        FValue = false;
        return FValue;
    }
    //得到集箱箱号数据
    p_cntr1 = CNTNO.substr(0, 4).toUpperCase();
    p_cntr2 = CNTNO.substr(4, 6);
    p_check = CNTNO.substr(10, 1);
    p_container = p_cntr1 + p_cntr2;
    //检查箱号前4位是否是字符型
    p_i = 0;
    FValue = true;
    while ((p_i <= 3) & FValue) {
        p_a = p_cntr1.charCodeAt(p_i);
        if (p_a <= 64 || p_a >= 91) {
            FValue = false;
            return FValue;
        }
        p_i = p_i + 1;
    }
    //          //检查箱号后6位是否是数字型
    p_i = 0;
    FValue = true;
    while ((p_i <= 5) & FValue) {
        p_a = p_cntr2.charCodeAt(p_i);
        if (p_a <= 47 || p_a >= 58) {
            FValue = false;
            return FValue;
        }
        p_i = p_i + 1;
    }
    //           //字母按不同的字母 (A―10，B―12.., L―23，..., V―34, ..., Z―38)
    //           //对应不同数字
    //           //数字等于自己本身乘上2的 (i-1) 次方，并累加在一起
    p_i = 0;
    p_sum = 0;
    p_power = 1;

    while (p_i <= 9) {
        if (p_i == 0) {
            p_power = 1;
        } else {
            p_power = 2 * p_power;
        }
        if (p_i <= 3) {
            p_a = p_container.charCodeAt(p_i) - 55;
            p_sum = p_sum + (p_a + parseInt(p_a / 10.1)) * p_power;
        } else {
            if (p_i == 4) {
                if (p_cntr1 == "HLCU") {
                    p_sum = p_sum - 2;
                }
            }
            p_sum = p_sum + parseInt(p_container.substr(p_i, 1)) * p_power;
        }
        p_i = p_i + 1;
    }
    //          //箱号的检查位=MOD (累加数/11) 如果得到的余数为10，检查位等于0
    //          //如余数为其他值，检查位应为余数
    p_check_i = p_sum % 11;

    if (p_check_i == 10) {
        p_check_i = 0;
    }
    var Result;
    try {
        if (p_check_i != p_check) {
            FValue = false;
        }
        Result = FValue;
    } catch (err) {
        Result = false;
    }
    return Result;
}

// 校验手机号
function validPhone(phone) {
    let reg_phone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
    if (!reg_phone.test(phone)) {
        return false
    }
    console.log('22222')
    return true
}
// 校验数字
function validNumber(number) {
    let reg_phone = /^[0-9]*$/
    if (!reg_phone.test(number)) {
        return false
    }
    return true
}
// 校验密码 密码8位及以上，包含字母大小写、数字
function validPassword(password) {
    // let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
    let reg = /^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]*$/;

    if (!reg.test(password)) {
        return false
    }
    return true
}


export {
    dateFormat,
    dateFormatYMD,
    FormatterDate,
    ValidCNTNO,
    validPhone,
    validNumber,
    validPassword,
}